package com.vdong.rms.service.impl;

import com.vdong.rms.common.exception.ProcessException;
import com.vdong.rms.entity.mo.Employee;
import com.vdong.rms.entity.mo.EmployeeExample;
import com.vdong.rms.entity.parameter.LoginPO;
import com.vdong.rms.entity.visual.EmployeeAuthTokenVO;
import com.vdong.rms.repository.dao.EmployeeMapper;
import com.vdong.rms.service.EmployeeOperationService;
import org.apache.commons.collections.CollectionUtils;
import org.lamb.framework.common.util.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//import javax.transaction.Transactional;

import java.util.List;

import static com.vdong.rms.common.enums.ProcessExceptionEnum.EB00000006;

/**
 * @description: 员工操作
 * @author: Mr.WangGang
 * @create: 2018-10-19 上午 11:43
 **/
@Service
public class EmployeeOperationServiceImpl implements EmployeeOperationService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public EmployeeAuthTokenVO login(LoginPO loginPO) {
        ValidatorUtil.validate(loginPO);

        EmployeeExample employeeExample = new EmployeeExample();
        employeeExample.createCriteria().andEmployeeAccountEqualTo(loginPO.getEmployeeAccount());

        List<Employee> employeeList = employeeMapper.selectByExample(employeeExample);

        if (CollectionUtils.isEmpty(employeeList)) {
            throw new ProcessException(EB00000006);
        }
        if (!loginPO.getAccountPassword().equals(employeeList.get(0).getAccountPassword())) {
            throw new ProcessException(EB00000006);
        }

        /*EmployeeDO employeeDO = employeeDORepository.findByEmployeeAccount(
                loginPO.getEmployeeAccount()).orElseThrow(() -> new ProcessException(EB00000006));
        if (!loginPO.getAccountPassword().equals(employeeDO.getAccounPassword())) {
            throw new ProcessException(EB00000006);
        }*/
        EmployeeAuthTokenVO employeeAuthTokenVO = new EmployeeAuthTokenVO();
        employeeAuthTokenVO.setAuthToken("TEST_ACCESS_TOKEN");
        return employeeAuthTokenVO;
    }
}
